package cn.edu.tju.elm.model;

import cn.edu.tju.core.model.BaseEntity;
import jakarta.persistence.*;
import java.math.BigDecimal;

/**
 * 钱包规则配置实体类
 * 用于配置充值返点、提现手续费等规则
 */
@Entity
@Table(name = "wallet_rule")
public class WalletRule extends BaseEntity {

    /**
     * 规则名称
     */
    @Column(name = "rule_name", nullable = false, length = 100)
    private String ruleName;

    /**
     * 规则类型：RECHARGE（充值）、WITHDRAW（提现）
     */
    @Column(name = "rule_type", nullable = false, length = 20)
    private String ruleType;

    /**
     * 最小金额阈值
     */
    @Column(name = "min_amount", precision = 10, scale = 2, nullable = false)
    private BigDecimal minAmount = BigDecimal.ZERO;

    /**
     * 最大金额阈值（null表示无上限）
     */
    @Column(name = "max_amount", precision = 10, scale = 2)
    private BigDecimal maxAmount;

    /**
     * 奖励金额（充值返点）或手续费金额（提现）
     */
    @Column(name = "bonus_amount", precision = 10, scale = 2)
    private BigDecimal bonusAmount;

    /**
     * 奖励比例（充值返点）或手续费比例（提现），范围0-1
     */
    @Column(name = "bonus_rate", precision = 5, scale = 4)
    private BigDecimal bonusRate;

    /**
     * 是否启用
     */
    @Column(name = "is_enabled", nullable = false)
    private Boolean isEnabled = true;

    /**
     * 规则描述
     */
    @Column(name = "description", length = 500)
    private String description;

    /**
     * 优先级，数字越大优先级越高
     */
    @Column(name = "priority", nullable = false)
    private Integer priority = 0;

    public WalletRule() {
    }

    // Getters and Setters
    public String getRuleName() {
        return ruleName;
    }

    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
    }

    public String getRuleType() {
        return ruleType;
    }

    public void setRuleType(String ruleType) {
        this.ruleType = ruleType;
    }

    public BigDecimal getMinAmount() {
        return minAmount;
    }

    public void setMinAmount(BigDecimal minAmount) {
        this.minAmount = minAmount;
    }

    public BigDecimal getMaxAmount() {
        return maxAmount;
    }

    public void setMaxAmount(BigDecimal maxAmount) {
        this.maxAmount = maxAmount;
    }

    public BigDecimal getBonusAmount() {
        return bonusAmount;
    }

    public void setBonusAmount(BigDecimal bonusAmount) {
        this.bonusAmount = bonusAmount;
    }

    public BigDecimal getBonusRate() {
        return bonusRate;
    }

    public void setBonusRate(BigDecimal bonusRate) {
        this.bonusRate = bonusRate;
    }

    public Boolean getEnabled() {
        return isEnabled;
    }

    public void setEnabled(Boolean enabled) {
        isEnabled = enabled;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    /**
     * 计算充值返点或提现手续费
     * @param amount 交易金额
     * @return 奖励/手续费金额
     */
    public BigDecimal calculateBonus(BigDecimal amount) {
        if (!isEnabled) {
            return BigDecimal.ZERO;
        }

        // 检查金额是否在规则范围内
        if (amount.compareTo(minAmount) < 0) {
            return BigDecimal.ZERO;
        }
        if (maxAmount != null && amount.compareTo(maxAmount) > 0) {
            return BigDecimal.ZERO;
        }

        // 计算奖励/手续费
        BigDecimal calculatedBonus = BigDecimal.ZERO;
        if (bonusRate != null && bonusRate.compareTo(BigDecimal.ZERO) > 0) {
            calculatedBonus = amount.multiply(bonusRate);
        }
        if (bonusAmount != null && bonusAmount.compareTo(BigDecimal.ZERO) > 0) {
            calculatedBonus = calculatedBonus.add(bonusAmount);
        }

        return calculatedBonus;
    }

    /**
     * 检查规则是否适用于给定金额
     * @param amount 金额
     * @return 是否适用
     */
    public boolean isApplicable(BigDecimal amount) {
        if (!isEnabled) {
            return false;
        }
        if (amount.compareTo(minAmount) < 0) {
            return false;
        }
        return maxAmount == null || amount.compareTo(maxAmount) <= 0;
    }
}

